﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.Windows.Media.Media3D;
using Grafika_WPF.Components;

namespace Grafika_WPF.Shapes
{
    class Sphere:IShape
    {
        public Point3D Center { get; set; }
        public double R { get; set; }
        public Color Color { get; set; }
        public IList<Triangle> Triangles { get; set; }

        public Sphere(Point3D center, Color color, double r)
        {
            Center = center;
            R = r;
            Color = color;
           Triangles=new List<Triangle>();
        }
        public void Triangulate(int trianglesCount)
        {
            var trianglesList = new List<Triangle>();
            double step = 360.0 / trianglesCount;
            for (int i = 0; i < trianglesCount; i++)
            {
                for (int j = 0; j < trianglesCount / 4; j++)
                {
                    //Rotacja pozioma
                    var horizontalRotateTransform = new RotateTransform3D();
                    horizontalRotateTransform.Rotation = new AxisAngleRotation3D(new Vector3D(0, 1, 0),( i-0.5) * step);

                    //Rotacja pionowa
                    var verticalRotateTransfrom = new RotateTransform3D();
                    verticalRotateTransfrom.Rotation = new AxisAngleRotation3D(new Vector3D(0, 0, 1), j * step);

                    //Transformacja pionowa pkt 
                    var a = verticalRotateTransfrom.Transform(new Point3D(R, 0, 0));

                    //Zmiana kąta
                    verticalRotateTransfrom.Rotation = new AxisAngleRotation3D(new Vector3D(0, 0, 1), (j + 1) * step);

                    //Transformacja pionowa pkt
                    var b = verticalRotateTransfrom.Transform(new Point3D(R, 0, 0));

                    //Transforamcja pkt trójkąta
                    var pkt1 = horizontalRotateTransform.Transform(a);
                    var pkt2 = horizontalRotateTransform.Transform(b);
                    //Zmiana kąta poziomo

                    horizontalRotateTransform.Rotation = new AxisAngleRotation3D(new Vector3D(0, 1, 0), (i + 0.5) * step);
                    var pkt3 = horizontalRotateTransform.Transform(a);
                    var pkt4 = horizontalRotateTransform.Transform(b);

                    trianglesList.Add(new Triangle()
                    {
                        A = pkt1,
                        B = pkt3,
                        C = pkt2,
                      
                    });

                    trianglesList.Add(new Triangle()
                    {
                        A = pkt2,
                        B = pkt3,
                        C = pkt4,
                        
                    });
                }
            }
            var bottomRotateTransform = new RotateTransform3D();

            bottomRotateTransform.Rotation = new AxisAngleRotation3D(new Vector3D(1, 0, 0), -180);
            var bottomTriangleList = trianglesList.Select(triangle => new Triangle()
                                                                          {
                                                                              A = bottomRotateTransform.Transform(triangle.A), B = bottomRotateTransform.Transform(triangle.B), C = bottomRotateTransform.Transform(triangle.C)
                                                                          }).ToList();
            //Odbijamy o 180 stopni
            trianglesList.AddRange(bottomTriangleList);
            //Przenoszenie środka kuli do Center
            var centerTranslate = new TranslateTransform3D(Center-new Point3D(0,0,0));

            Triangles= trianglesList.Select(triangle => new Triangle()
            {
                A = centerTranslate.Transform(triangle.A),
                B = centerTranslate.Transform(triangle.B),
                C = centerTranslate.Transform(triangle.C),
                Color = Color
            }).ToList();
        }


    }
}
